package example.dawangba.com.exp4_z09315222;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class ResultActivity extends AppCompatActivity {

    private int[] sceImagesSrc = {R.mipmap.p1,R.mipmap.p2,R.mipmap.p3,R.mipmap.p4};

    private String[] sceNames = {"白云山","九寨沟","故宫博物院","黄山"};

    private String[] sceFile = {"t1.txt","t2.txt","t3.txt","t4.txt"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        ImageView imageView = (ImageView) findViewById(R.id.sceImage);
        TextView sceContext = (TextView) findViewById(R.id.sceContext);
        TextView sceName = (TextView) findViewById(R.id.sceName);

        Intent intent = getIntent();
        int index = intent.getIntExtra("position",-1);
        imageView.setImageResource(sceImagesSrc[index]);
        sceName.setText(sceNames[index]);

        String flag = readFromAssets(sceFile[index]);
        sceContext.setText(flag);
    }

    public String readFromAssets(String fname){
        String result = "";
        try {
            InputStream in = getResources().getAssets().open(fname);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            result = new String(buffer,"gbk");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}
